home *** CD-ROM | disk | FTP | other *** search
- Path: magnus.acs.ohio-state.edu!csn!ub!newserve!rebecca!rpi!not-for-mail
- From: jamshid@io.com (Jamshid Afshar)
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.tools.mfc,comp.lang.c++.moderated
- Subject: Re: Newbie question: default op=
- Followup-To: comp.os.ms-windows.programmer.tools.mfc,comp.lang.c++.moderated
- Date: 5 Feb 1996 21:28:46 -0000
- Organization: Illuminati Online, Austin, Texas, USA
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: vandevod@cs.rpi.edu
- Message-ID: <4f5sqe$dgm@netlab.cs.rpi.edu>
- References: <30FEA0B4.4F66@ymi.com> <4dmrdu$niv@news2.aimnet.com>
- NNTP-Posting-Host: netlab.cs.rpi.edu
- X-Original-Date: 20 Jan 1996 13:01:30 -0600
-
- { Note that follow-ups are to c.l.c++.m and c.o.m.p.tools.mfc -mod }
-
- In article <4dmrdu$niv@news2.aimnet.com>, Dan Howard <dhoward@kset.com> wrote:
- >Sadly, you are correct. You do need to write an explicit operator =()
- >because the compiler
- >does not provide one. Yes, the compiler should be able to figure it out but
- >it isn't in the
- >Draft ANSI C++ Specification so too bad.
-
- No, since at least 1990 when the ARM (Stroustrup's _Annotated C++
- Reference Manual) was first printed, C++ has required the compiler to
- generate a default assignment operator which calls the base class
- assignment operator then does a member-wise assignment. This is not
- (necessarily) the same as a bit-wise copy. This default assignment
- operator is not generated if any base class has a non-public
- assignment operator. The original poster was probably deriving from
- MFC's CObject, which declares CObject::operator=() as private, hence
- he had to explicitly define his derived class's operator=(). The docs
- imply that this was done on purpose. Don't necessarily take anything
- you see in the MFC as good or modern C++ code design practices.
-
- >If you want an even worse problem, the copy constructor automatically
- >provided by compilers
- >does a bit-copy rather than a member-by-member copy. Thus, no copy
- >constructors are called in
- >the following code:
-
- Again, this was true in early versions of C++ and with a few early
- compilers, but since the ARM the default copy constructor has done
- member-wise initialization, not a bit-wise copy.
-
- >class stupid { CString x; };
- >
- >void main() {
- > stupid a;
- > stupid b(a); // try stepping into this line,
- no CString::CString(const CString& s) called
- >}
-
- I believe Borland's first C++ compiler (c. 1990) implemented this
- correctly, so I would be surprised if Micrsoft's Visual C++ has this
- bug (and it takes a lot to surprise me when it comes to MS's C++
- implementation).
-
- >Jeffrey Keays <keays@ymi.com> wrote:
- >>I am trying to use the &*%$ MFC collections [...]
- >>I have this general C++ question.
- >>Why is it making me create operator= functions for assignment of the same
- >>type (or instance / reference of the same type). The class I'm trying to
- >>assign has no pointers or anything that would require special handling.
-
- You wouldn't have this problem if you didn't derive from MFC's
- CObject. Actually, using Smalltalk-like Object-based classes is
- widely regarded as bad C++ class design. You might try using MFC
- 4.0's template collections; they're a little better, but still pretty
- bizarre and poorly/incorrectly documented.
-
- Again, I can't stress enough to C++ programmers to not consider MFC an
- example of good C++ class design. Btw, if you do use CArray<T> note
- that it does not define an assignment operator (you have to use a
- Copy() member function) and note that the docs are incorrect about the
- template functions ConstructElements(T* arr, int nCount) and
- DestructElements(T* arr, int nCount). You do not have to specialize
- them -- they call the constructors/destructors, but you do have to
- specialize an UNDOCUMENTED template function CopyElements(T* dst, T*
- src, int nCount) because it defaults to a *bit-wise* copy.
-
- Jamshid Afshar
- jamshid@io.com
-
- [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
- [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
- [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
- [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
-